<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

bigtitle($txt['bnt_ze_title']);

$zone = (isset($_GET['zone'])) ? $_GET['zone'] : die();

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_zones}
	WHERE zone_id = {int:zone}
	LIMIT 1',
	array(
		'db_zones' => $dbtables['zones'],
		'zone' => $zone
	)
);

if($smcFunc['db_num_rows']($result) == 0)
	zoneedit_die($txt['bnt_zi_nexist']);
	
$curzone = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($curzone['corp_zone'] == 'N')
{
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}
		LIMIT 1',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$ownerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
}
else
{
	$result = $smcFunc['db_query']('', '
		SELECT creator, id 
		FROM {raw:db_teams}
		WHERE creator = {int:owner}
		LIMIT 1',
		array(
			'db_teams' => $dbtables['teams'],
			'owner' => $curzone['owner']
		)
	);
	$ownerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
}

if (($curzone['corp_zone'] == 'N' && $curzone['owner'] != $ownerinfo['ship_id']) || ($curzone['corp_zone'] == 'Y' && $curzone['owner'] != $ownerinfo['id'] && $curzone['owner'] == $ownerinfo['creator']))
	zoneedit_die($txt['bnt_ze_notowner']);

if (isset($_GET['command']) && $_GET['command'] == 'change')
{
	$name = $_POST['name'];
	$beacons = $_POST['beacons'];
	$attacks = $_POST['attacks'];
	$warpedits = $_POST['warpedits'];
	$planets = $_POST['planets'];
	$trades = $_POST['trades'];
	$defenses = $_POST['defenses'];
	zoneedit_change($zone,$name,$beacons,$attacks,$warpedits,$planets,$trades,$defenses);
}

$ybeacon = "";
$nbeacon = "";
$lbeacon = "";
$yattack = "";
$nattack = "";
$ywarpedit = "";
$nwarpedit = "";
$lwarpedit = "";
$yplanet = "";
$nplanet = "";
$lplanet = "";
$ytrade = "";
$ntrade = "";
$ltrade = "";
$ydefense = "";
$ndefense = "";
$ldefense = "";

if ($curzone['allow_beacon'] == 'Y')
	$ybeacon = "checked";
elseif ($curzone['allow_beacon'] == 'N')
	$nbeacon = "checked";
else
	$lbeacon = "checked";

if ($curzone['allow_attack'] == 'Y')
	$yattack = "checked";
else
	$nattack = "checked";

if ($curzone['allow_warpedit'] == 'Y')
	$ywarpedit = "checked";
elseif ($curzone['allow_warpedit'] == 'N')
	$nwarpedit = "checked";
else
	$lwarpedit = "checked";

if ($curzone['allow_planet'] == 'Y')
	$yplanet = "checked";
elseif ($curzone['allow_planet'] == 'N')
	$nplanet = "checked";
else
	$lplanet = "checked";

if ($curzone['allow_trade'] == 'Y')
	$ytrade = "checked";
elseif ($curzone['allow_trade'] == 'N')
	$ntrade = "checked";
else
	$ltrade = "checked";

if ($curzone['allow_defenses'] == 'Y')
	$ydefense = "checked";
elseif ($curzone['allow_defenses'] == 'N')
	$ndefense = "checked";
else
	$ldefense = "checked";

echo '	<form action="', $scripturl, '?action=blacknova;sa=zoneedit;command=change;zone=', $zone, '" method="post">
		<table border="0">
			<tr>
				<td align="right"><font size="2"><b>', $txt['bnt_ze_name'], ' : &nbsp;</b></font></td>
				<td><input type="text" name="name" size="30" maxlength="30" value="', $curzone['zone_name'], '"></td>
			</tr>
			<tr>
				<td align="right"><font size="2"><b>', $txt['bnt_ze_allow'], ' ', $txt['bnt_beacons'], ' : &nbsp;</b></font></td>
				<td><input type="radio" style="check" name="beacons" value="Y" ', $ybeacon, '>&nbsp;', $txt['bnt_yes'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="beacons" value="N" ', $nbeacon, '>&nbsp;', $txt['bnt_no'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="beacons" value="L" ', $lbeacon, '>&nbsp;', $txt['bnt_zi_limit'], '</td>
			</tr>
			<tr>
				<td align="right"><font size="2"><b>', $txt['bnt_ze_attacks'], ' : &nbsp;</b></font></td>
				<td><input type="radio" style="check" name="attacks" value="Y" ', $yattack, '>&nbsp;', $txt['bnt_yes'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="attacks" value="N" ', $nattack, '>&nbsp;', $txt['bnt_no'], '</td>
			</tr>
			<tr>
				<td align="right"><font size="2"><b>', $txt['bnt_ze_allow'], ' ', $txt['bnt_warpedit'], ' : &nbsp;</b></font></td>
				<td><input type="radio" style="check" name="warpedits" value="Y" ', $ywarpedit, '>&nbsp;', $txt['bnt_yes'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="warpedits" value="N" ', $nwarpedit, '>&nbsp;', $txt['bnt_no'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="warpedits" value="L" ', $lwarpedit, '>&nbsp;', $txt['bnt_zi_limit'], '</td>
			</tr>
			<tr>
				<td align="right"><font size="2"><b>', $txt['bnt_ze_allow'], ' ', $txt['bnt_sector_def'], ' : &nbsp;</b></font></td>
				<td><input type="radio" style="check" name="defenses" value="Y" ', $ydefense, '>&nbsp;', $txt['bnt_yes'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="defenses" value="N" $ndefense>&nbsp;', $txt['bnt_no'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="defenses" value="L" ', $ldefense, '>&nbsp;', $txt['bnt_zi_limit'], '</td>
			</tr>
			<tr>
				<td align="right"><font size="2"><b>', $txt['bnt_ze_genesis'], ' : &nbsp;</b></font></td>
				<td><input type="radio" style="check" name="planets" value="Y" ', $yplanet, '>&nbsp;', $txt['bnt_yes'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="planets" value="N" ', $nplanet, '>&nbsp;', $txt['bnt_no'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="planets" value="L" ', $lplanet, '>&nbsp;', $txt['bnt_zi_limit'], '</td>
			</tr>
			<tr>
				<td align="right"><font size="2"><b>', $txt['bnt_ze_allow'], ' ', $txt['bnt_title_port'], ' : &nbsp;</b></font></td>
				<td><input type="radio" style="check" name="trades" value="Y" ', $ytrade, '>&nbsp;', $txt['bnt_yes'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="trades" value="N" ', $ntrade, '>&nbsp;', $txt['bnt_no'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" style="check" name="trades" value="L" ', $ltrade, '>&nbsp;', $txt['bnt_zi_limit'], '</td>
			</tr>
			<tr>
				<td colspan="2" align="center"><br><input type="submit" value=', $txt['bnt_submit'], '></td>
			</tr>
		</table>
		</form>';


echo '<p><a href="', $scripturl, '?action=blacknova;sa=zoneinfo;zone=', $zone, '">', $txt['bnt_clickme'], '</a> ', $txt['bnt_ze_return'], '.<p>';
TextMainMenu();

//-----------------------------------------------------------------

function zoneedit_change($zone,$name,$beacons,$attacks,$warpedits,$planets,$trades,$defenses)
{
	global $smcFunc, $txt, $scripturl;
	global $dbtables;

	if (!get_magic_quotes_gpc())
		$name = addslashes($name);
	
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_zones}
		SET zone_name = {string:name},
			allow_beacon = {string:beacons},
			allow_attack = {string:attacks},
			allow_warpedit = {string:warpedits},
			allow_planet = {string:planets},
			allow_trade = {string:trades},
			allow_defenses = {string:defenses}
		WHERE zone_id = {int:zone_id}',
		array(
			'db_zones' => $dbtables['zones'],
			'name' => $name,
			'beacons' => $beacons,
			'attacks' => $attacks,
			'warpedits' => $warpedits,
			'planets' => $planets,
			'trades' => $trades,
			'defenses' => $defenses,
			'zone_id' => $zone
		)
	);

	echo '<p>', $txt['bnt_ze_saved'], '</p>';
	echo '<p><a href=', $scripturl, '?action=blacknova;sa=zoneinfo;zone=', $zone, '>', $txt['bnt_clickme'], '</a> ', $txt['bnt_ze_return'], '<p>';
	TextMainMenu();
	return;
}

function zoneedit_die($error_msg)
{
	echo '<p>', $error_msg, '</p>';
	TextMainMenu();
	return;
}
?>
